//package fileupdown;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.MultipartConfig;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.Part;
//import java.io.*;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.util.UUID;
//
//@WebServlet("/upload")
//@MultipartConfig
//public class FileUpload extends HttpServlet {
//    public static final String URL = "http://localhost:8080/resourse";
//
//    @Override
//    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        // 获取文件部分
//        Part part = req.getPart("file");
//        //获取上传的文件名
//        String submittedFileName = part.getSubmittedFileName();
//        // 生成唯一的文件ID
//        String fileId = UUID.randomUUID().toString();
//        // 获取文件内容流
//        InputStream fileContent = part.getInputStream();
//        String url = URL+submittedFileName;
//        OutputStream outputStream = new
//                FileOutputStream("D:\\CodeAll\\code\\FirstWeb\\src\\main\\webapp\\resourse\\"+submittedFileName);
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while (-1!= (len =fileContent.read(bytes) )){
//            outputStream.write(bytes,0,len);
//        }
//        fileContent.close();
//        outputStream.flush();
//        try {
//            Connection connection = JdcbConnection.requestConnection();
//            String sql = "INSERT INTO file_table (file_id,attachment_name, url, real_name) " +
//                    "VALUES (?,?,?,?) ";
//            PreparedStatement preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setString(1,fileId);
//            preparedStatement.setString(2,submittedFileName);
//            preparedStatement.setString(3,url);
//            preparedStatement.setString(4,submittedFileName);
//            preparedStatement.executeUpdate();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
////        resp.setContentType("text/html;charset=UTF-8");
////        PrintWriter writer = resp.getWriter();
////        writer.print("<html>");
////        writer.print("<body>");
////        writer.print("<h2>文件上传成功!<h2>");
////        writer.print("<h2>3s后返回下载列表<h2>");
////        writer.print("</body>");
////        writer.print("</html>");
////        System.out.println("打印上传成功");
//
////        resp.setHeader("Refresh", "3; url=index.jsp");
////        resp.setHeader("Refresh", "3; url=./index");
//        resp.sendRedirect("/index");
//    }
//
//}
